(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

include List
  
let rec take n l =
  if n = 0 then
    []
  else
    match l with
      | [] -> []
      | x :: xs -> x :: (take (n - 1) xs)
  
let rec drop n l =
  if n = 0 then l else (match l with | [] -> [] | _ :: xs -> drop (n - 1) xs)
  
let rec last l =
  match l with
    | [] -> failwith "XList.last: list empty!"
    | [ x ] -> x
    | _ :: xs -> last xs
  
let rec split_at n l =
  if n = 0
  then ([], l)
  else
    match l with
      | [] -> ([], [])
      | x :: _ ->
          let (a, b) = split_at (n - 1) x in
            ((x :: a), b)
  
let rec take_while p l =
  match l with
  | [] -> []
  | x :: xs ->
      if p x then
        x :: (take_while p xs)
      else
        []
  
let rec drop_while p l =
  match l with
    | [] -> []
    | x :: xs ->
        if p x then
          drop_while p xs
        else
          l
  
let rec span p l =
  match l with
    | [] -> ([], [])
    | x :: xs ->
        if p x then
          let (a, b) = span p xs in
            ((x :: a), b)
        else
          ([], l)
  
let rec break p l =
  match l with
    | [] -> ([], [])
    | x :: xs ->
        if p x then
          ([], l)
        else
          let (a, b) = span p xs in
            ((x :: a), b)
  
let rec repeat n x =
  let rec iter n =
    if n <= 0 then
      []
    else
      x :: (iter (n - 1))
  in
    iter n
  
let rev_to_array x =
  match x with
    | [] -> [|  |]
    | y :: ys ->
        let len = List.length x in
        let arr = Array.make len y in
        let rec iter i x =
          match x with
            | [] -> arr
            | y :: ys ->
                arr.(i) <- y;
                iter (i - 1) ys
        in
          iter (len - 2) ys
  
let rec append_sub_array arr from_pos to_pos tail =
  let rec iter tail to_pos =
    if from_pos > to_pos then
      tail
    else
      iter (arr.(to_pos) :: tail) (to_pos - 1)
  in
    iter tail to_pos
  
let rec from_sub_array arr from_pos to_pos =
  append_sub_array arr from_pos to_pos []
